package com.kiseny.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;

import com.common.util.CookieUtils;
import com.kiseny.entity.UserInfo;
import com.kiseny.inout.AuthenticationInput;
import com.kiseny.inout.AuthenticationOutput;
import com.kiseny.po.Account;

public class LoginAction extends BaseAction{
    
    private static final long serialVersionUID = -4775933495436838278L;

    public String input () {
        if (getAccount() != null) {
            getAccount().setName(STRING_EMPTY);
        }
        return INPUT;
    }
    
    public String login () {

        if (getAccount() != null) {

            String name = getAccount().getName();
            String password = getAccount().getPassword();

            if (!StringUtils.isBlank(name) && !StringUtils.isBlank(password)) {

                String newUserName = name.trim();

                AuthenticationInput authenInput = new AuthenticationInput();
                authenInput.setName(newUserName);
                authenInput.setPassword(password);
                authenInput.setAutoLoginFlag(getAccount().isAddDefaultFlag());

                AuthenticationOutput authenOutput = getAuthenticationService().verifyAccount(authenInput);
                Account account = authenOutput.getAccount();

                // If account is null, of course. it is Invalid.
                if (account != null && authenOutput.isValidFlg()) {
                    if (account.isAddDefaultFlag()) {
                        Cookie cookie = cookieUtils.addCookie(account);
                        getResponse().addCookie(cookie); // add cookie to response.
                    }
                    // add account into session.
                    getSession().put(ACCOUNT_SESSION, account);
                    setAccount(account);
                    return LOGIN;
                } else {
                    List<Object> args = new ArrayList<Object>();
                    args.add(authenInput.getName());
                    addActionError(getText(ERROR_INVALID_PASSWORD, args));
                    setLoginFailedFlag(true);
                    return FAILED;
                }
            }
        }
        addActionError(getText(ERROR_USERNAME_PASSWORD_EMPTY));
        setLoginFailedFlag(true);
        return FAILED;   
    }

    public String logout() {
    	
      if (getSession() != null) {
          getSession().remove(ACCOUNT_SESSION);  
        }
        Cookie cookie = cookieUtils.deleteCookie(getRequest());  
        if (cookie != null)  {
            getResponse().addCookie(cookie);  
        }
        
        
        return LOGOUT;
    }

    private boolean autoLoginFlag;
    private CookieUtils cookieUtils = new CookieUtils();

    public boolean isAutoLoginFlag() {
        return autoLoginFlag;
    }

    public void setAutoLoginFlag(boolean autoLoginFlag) {
        this.autoLoginFlag = autoLoginFlag;
    }
    
    
	private ArrayList<UserInfo> userList = new ArrayList<UserInfo>();

	public ArrayList<UserInfo> getUserList() {
		return userList;
	}
}